package com.zhihuichongcheng.application.util;

import android.database.Cursor;
import android.support.annotation.NonNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by admin on 2016/3/24.
 */
public class CollectionUtils {
    /**
     * 遍历整数
     */
    public static void iterator(@NonNull int size, AIIteratorCallBack callBack) {
        if (null == callBack) return;
        for (int i = 0; i < size; i++) {
            boolean finish = callBack.callback(i);
            if (finish) {
                return;
            }
        }
    }

    /**
     * 遍历Set
     */
    public static void iterator(@NonNull Set date, AIIteratorListCallBack callBack) {
        if (null == callBack || null == date) return;
        Iterator<Object> it = date.iterator();
        int position = 0;
        while (it.hasNext()) {
            Object object = it.next();
            callBack.callback(object, position);
            position++;
        }
    }

    /**
     * 遍历Map
     */
    public static void iterator(@NonNull List dates, AIIteratorListCallBack callBack) {
        if (isEmpty(dates)) return;
        for (int i = 0; i < dates.size(); i++) {
            Object data = dates.get(i);
            if (null != callBack) {
                boolean finish = callBack.callback(data, i);
                if (finish) return;
            }
        }
    }

    /**
     * 判空
     */
    public static boolean isEmpty(Collection list) {
        return null == list || list.size() == 0;
    }

    /**
     * 遍历数组
     */
    public static void iterator(@NonNull Object[] dates, AIIteratorListCallBack callBack) {
        if (isEmpty(dates)) throw new NullPointerException();
        for (int i = 0; i < dates.length; i++) {
            Object data = dates[i];
            if (null != callBack) {
                boolean finish = callBack.callback(data, i);
                if (finish) return;
            }
        }
    }

    /**
     * 判空
     */
    public static boolean isEmpty(Object[] list) {
        return null == list || list.length == 0;
    }

    /**
     * 遍历数组
     */
    public static void iterator(@NonNull float[] dates, AIIteratorListCallBack callBack) {
        if (isEmpty(dates)) throw new NullPointerException();
        for (int i = 0; i < dates.length; i++) {
            Object data = dates[i];
            if (null != callBack) {
                boolean finish = callBack.callback(data, i);
                if (finish) return;
            }
        }
    }

    /**
     * 判空
     */
    public static boolean isEmpty(float[] list) {
        return null == list || list.length == 0;
    }

    /**
     * 遍历Cursor
     */
    public static void iterator(@NonNull Cursor cursor, AIIteratorCursorCallBack callBack) {
        int position = 0;
        while (cursor.moveToNext()) {
            if (null != callBack) {
                boolean finish = callBack.callback(cursor, position);
                if (finish) {
                    cursor.close();
                    return;
                }
            }
            position++;
        }
        cursor.close();
    }

    /**
     * 取map1与map2比 新增加的项
     *
     * @param map1 原始数据 {1,2,3}
     * @param map2 排除的数据{2,3}
     * @return {1}
     */
    public static Map<Object, Object> findNewItems(@NonNull final Map<Object, Object> map1, @NonNull Map<Object, Object> map2) {
        final Map<Object, Object> result = new LinkedHashMap<>();
        result.putAll(map1);
        iterator(map2, new AIIteratorMapCallBack() {
            @Override
            public boolean callback(Object key, Object value, int position) {
                if (result.containsKey(key)) {
                    result.remove(key);
                }
                return false;
            }
        });
        return result;
    }

    /**
     * 遍历Map
     */
    public static void iterator(@NonNull Map date, AIIteratorMapCallBack callBack) {
        if (null == callBack) return;
        Iterator iterator = date.entrySet().iterator();
        int i = 0;
        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            Object key = entry.getKey();
            Object value = entry.getValue();
            boolean finish = callBack.callback(key, value, i);
            if (finish) {
                return;
            }
            i++;
        }
    }

    /**
     * 取map2与map1比 被删减的项目
     *
     * @param map1 原始数据 {1，2，3}
     * @param map2 排除的数据{2，3}
     * @return {1}
     */
    public static Map<Object, Object> findDelItems(@NonNull Map<Object, Object> map1, @NonNull Map<Object, Object> map2) {
        final Map<Object, Object> result = new LinkedHashMap<>();
        result.putAll(map1);
        iterator(map2, new AIIteratorMapCallBack() {
            @Override
            public boolean callback(Object key, Object value, int position) {
                if (result.containsKey(key)) {
                    result.remove(key);
                }
                return false;
            }
        });
        return result;
    }

    /**
     * 删除多个项目
     *
     * @param currentMap 当前
     * @param removeMap  被删除
     * @return 结果
     */
    public static void removeAll(@NonNull final Map<Object, Object> currentMap, Map<Object, Object> removeMap) {
        if (isEmpty(removeMap) || isEmpty(currentMap)) return;
        iterator(removeMap, new AIIteratorMapCallBack() {
            @Override
            public boolean callback(Object key, Object value, int position) {
                if (currentMap.containsKey(key)) {
                    currentMap.remove(key);
                }
                return false;
            }
        });
    }

    /**
     * 判空
     */
    public static boolean isEmpty(Map list) {
        return null == list || list.size() == 0;
    }

    public static <T extends Object> List<T> getKeyList(Map<T, String> data) {
        final List<T> result = new ArrayList<>();
        iterator(data, new AIIteratorMapCallBack() {
            @Override
            public boolean callback(Object key, Object value, int position) {
                result.add((T) key);
                return false;
            }
        });
        return result;
    }

    public static <K extends Object, V extends Object> ArrayList<V> getValueList(Map<K, V> data) {
        final ArrayList<V> result = new ArrayList<>();
        iterator(data, new AIIteratorMapCallBack() {
            @Override
            public boolean callback(Object key, Object value, int position) {
                result.add((V) value);
                return false;
            }
        });
        return result;
    }

    public interface AIIteratorCallBack {
        boolean callback(int position);
    }


    public interface AIIteratorMapCallBack {
        boolean callback(Object key, Object value, int position);
    }

    public interface AIIteratorListCallBack {
        boolean callback(Object data, int position);
    }

    public interface AIIteratorCursorCallBack {
        boolean callback(Cursor cursor, int position);
    }
}
